//
//  YPUserRootHeaderView.m
//  Youpin
//
//  Created by xiaowuxiaowu on 2017/4/13.
//  Copyright © 2017年 xiaowuxiaowu. All rights reserved.
//

#import "YPUserRootHeaderView.h"
#import "TPButton.h"
@interface YPUserRootHeaderView()

@property (nonatomic,strong)UILabel *userNameLabel;
@property (nonatomic,strong)UIButton *ediorButton;
@end
@implementation YPUserRootHeaderView
-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
     
        [self setup];
    }
    
    return self;
}
-(void)setup{
 
    UIImageView *bgView = [[UIImageView alloc]initWithImage:ImageNamed(@"YP_user_header_bg")];
    bgView.clipsToBounds= YES;
    bgView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 82.0, 0));
    }];
    
    self.backgroundColor = [UIColor whiteColor];
    @weakify(self);
    
    self.image_view.contentMode = UIViewContentModeScaleAspectFill;
    self.image_view.userInteractionEnabled = YES;
    self.image_view.tag = YPUserRootHeaderViewButtonStyleActor;
    [self.image_view addTapGestureRecognizer:^(UITapGestureRecognizer* recognizer, NSString* gestureId) {
        @strongify(self);
        self.actionButtonBlock(recognizer.view.tag, nil);
    }];
   
    _userNameLabel = [UILabel initWithFrame:CGRectZero font:fontSystemOfSize(17) color:[UIColor whiteColor] aligment:NSTextAlignmentLeft];
    [self addSubview:_userNameLabel];
    
    self.detail_label.font = fontSystemOfSize(17);
    self.detail_label.textColor = [UIColor whiteColor];
    
    _ediorButton = [UIButton buttonWithFrame:CGRectZero imageName:@"TP_user_header_edior_icon"];
    _ediorButton.tag = YPUserRootHeaderViewButtonStyleEdior;
    _ediorButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:_ediorButton];
    [_ediorButton addActionHandler:^(NSInteger tag, UIButton *button) {
        @strongify(self);
        self.actionButtonBlock(tag,button);
    }];
    
    ViewBorderRadius(self.image_view, 80.0/2,2.0,[UIColor colorWithWhite:1.0 alpha:0.8]);
    [self.image_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15.0);
        make.size.mas_equalTo(CGSizeMake(80.0, 80.0));
        make.centerY.mas_equalTo(self).offset(-20.0);
    }];
    
    [_userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.image_view.mas_centerY).offset(-15.0);
        make.left.mas_equalTo(self.image_view.mas_right).offset(10.0);;
        make.width.mas_equalTo(200.0f);
        make.height.mas_equalTo(30.0);
        
    }];
    
    [self.detail_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_userNameLabel.mas_bottom);
        make.left.mas_equalTo(self.userNameLabel.mas_left);
        make.width.mas_equalTo(200.0f);
        make.height.mas_equalTo(_userNameLabel.mas_height);
        
    }];
    
    [_ediorButton mas_makeConstraints:^(MASConstraintMaker *make) {
         make.centerY.mas_equalTo(self.image_view.mas_centerY);
        make.right.mas_equalTo(-20.0);
        make.size.mas_equalTo(CGSizeMake(20.0, 20.0));
    }];
    
 
    
//    [self setBuss];
    [self setPerson];
}
-(void)setPerson{
   

      @weakify(self);
    NSArray *nImgs = @[@"TP_user_header_order_icon",@"TP_user_header_booking_icon",@"TP_user_header_nursing_icon"];

    NSArray *nTitles = @[@"我的订单",@"我的预约",@"护理记录"];
    UIButton *testButton;
    for(NSInteger i = 0; i<nImgs.count; i++){

        TPButton *button = [TPButton buttonWithFrame:CGRectZero title:nTitles[i] font:fontSystemOfSize(16.0) color:[UIColor blackColor]];
        button.aligmentType = TPButtonAlignmentTypeCenterUp;
        [button setImage:ImageNamed(nImgs[i]) forState:UIControlStateNormal];
        button.tag = i+YPUserRootHeaderViewButtonStyleOrder;
        [button addActionHandler:^(NSInteger tag, UIButton *button) {
            @strongify(self);
            self.actionButtonBlock(tag,button);
        }];
        [self addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {

            if(testButton){
                make.left.mas_equalTo(testButton.mas_right);
            }else{
                make.left.mas_equalTo(10.0f);
            }
            make.width.mas_equalTo(self).multipliedBy(0.32);
            make.height.mas_equalTo(82);
            make.bottom.mas_equalTo(0.0);

        }];

//        [button setImageEdgeInsets:UIEdgeInsetsMake(0.0, 42.0, 24, 0.0)];
//        [button setTitleEdgeInsets:UIEdgeInsetsMake(30.0, 0.0, 0.0, 23.0)];



        testButton = button;

    }
}
//
//-(void)setBuss{
//
//
////    for(UIView *view in self.image_view.subviews){
////        if([view isKindOfClass:[UIButton class]]){
////            [view removeFromSuperview];
////        }
////    }
//
////    NSArray *nImgs = @[@"YP_user_collection_icon",@"YP_user_header_fabu_icon"];
//    @weakify(self);
//    NSArray *nTitles = @[@"护理记录",@"我的预约"];
//    for(NSInteger i = 0; i<nTitles.count; i++){
//
//        TPButton *button = [TPButton buttonWithFrame:CGRectZero title:nTitles[i] font:fontSystemOfSize(17.0) color:TPBlack_333333];
////         button.aligmentType = TPButtonAlignmentTypeCenterUp;
////        [button setImage:ImageNamed(nImgs[i]) forState:UIControlStateNormal];
//        button.tag = i+YPUserRootHeaderViewButtonStyleNursing;
//        [button addActionHandler:^(NSInteger tag, UIButton *button) {
//            @strongify(self);
//            self.actionButtonBlock(tag,button);
//        }];
//        [self.image_view addSubview:button];
//        [button mas_makeConstraints:^(MASConstraintMaker *make) {
//
//            if(i == 1){
//                make.right.mas_equalTo(0.0);
//            }else{
//                make.left.mas_equalTo(0.0f);
//            }
//            make.width.mas_equalTo(self.image_view).multipliedBy(0.5);
//            make.top.bottom.mas_equalTo(0.0);
//
//        }];
//    }
//
//    UIImageView *line = [[UIImageView alloc]init];
//    line.backgroundColor = TP_COLOR_245;
//    [self.image_view addSubview:line];
//    [self.image_view bringSubviewToFront:line];
//    [line mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.mas_equalTo(self.mas_centerX);
//        make.height.mas_equalTo(self.image_view.mas_height).multipliedBy(0.7);
//        make.centerY.mas_equalTo(self.image_view);
//        make.width.mas_equalTo(1.5);
//    }];
//
//}

-(void)setModel:(LoginModel *)model{
    _model = model;
    _userNameLabel.text = xw_getNULLString(model.alias);//xw_getNULLString(model.name);
    self.detail_label.text = xw_getNULLString(model.mobile);
    [self.image_view ym_setImageWithURL:model.head placeholderImage:Tripvi_avtor];
}

@end
